package com.mjk.common.core.jdbc.ibatis.typehandler.handler;


import com.mjk.common.base.model.SystemDate;
import com.mjk.common.core.jdbc.ibatis.typehandler.BaseHandler;
import com.mjk.common.core.jdbc.service.plus.provider.BeeDataSourceType;
import com.mjk.common.core.jdbc.util.TableUtils;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TimeUtils;

import java.lang.reflect.Field;
import java.util.Date;

public class DateHandler implements BaseHandler<Date> {

	@Override
	public String resovleToString(Field f,Date t) {
		if(ObjectUtils.isEmpty(t)){
            return null;
		}
		if(t instanceof SystemDate){
			return "sysdate";
		}
		String driverName = TableUtils.getDriverName();
		if(driverName.contains(BeeDataSourceType.ORACLE_WRAP.getDriver()) || driverName.contains(BeeDataSourceType.DM_WRAP.getDriver())){
			if(t.getHours()==0 && t.getMinutes()==0 && t.getSeconds()==0){
				return "to_date('" + TimeUtils.formatTime(t, TimeUtils.DATE_FORMAT_DATEONLY) + "','yyyy-mm-dd')";
			}else{
				return "to_date('" + TimeUtils.formatTime(t, TimeUtils.DATE_FORMAT_DATETIME) + "','yyyy-mm-dd hh24:mi:ss')";
			}
			
		}
		return "'"+TimeUtils.formatTime(t, TimeUtils.DATE_FORMAT_DATETIME)+"'";			
	}

	@Override
	public Date resovleResult(Object o) {
		if(o instanceof Date date) {
			return date;
		}
		return TimeUtils.parse(o.toString());
	}

	
}
